layui.config({
    base: ctx + '/layuiadmin/' //静态资源所在路径
}).extend({
    index: 'lib/index'         //主入口模块
}).use(['index', 'table', 'util'], function () {
    var prefix = ctx + "/manage/paper/paper";
    var $ = layui.$, form = layui.form, table = layui.table, util = layui.util;

    //监听搜索
    form.on('submit(LAY-search)', function (data) {
        var field = data.field;
        //执行重载
        table.reload('LAY-table', {
            where: field
        });
    });

    //事件
    var active = {
        batchdel: function () {
            // 遍历所有选择的行数据，取每条数据对应的ID
            var checkStatus = table.checkStatus('LAY-table')
                , checkData = checkStatus.data; //得到选中的数据
            if (checkData.length === 0) {
                return commonJs.openMsg('warning', '请选择要删除的数据！');
            }
            var ids = new Array();
            $.each(checkData, function (i, row) {
                ids[i] = row['id'];
            });

            commonJs.openConfirm('确定删除吗？', function () {
                commonJs.openAjax(prefix + "/batchRemove", {"ids": ids}, function (result) {
                    if (result.code == 0) {
                        commonJs.openMsg('success', '删除成功！', function () {
                            table.reload('LAY-table'); //数据刷新
                        });
                    } else {
                        commonJs.openMsg('error', result.msg);
                    }
                });
            });
        },
        add: function () {
            commonJs.openDialog('添加', prefix + '/add', {}, [], function (index, layero) {
                var iframeWindow = window['layui-layer-iframe' + index]
                    , submitID = 'LAY-operate-submit'
                    , submit = layero.find('iframe').contents().find('#' + submitID);

                //监听提交
                iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                    commonJs.openAjax(prefix + "/save", data, function (result) {
                        if (result.code == 0) {
                            commonJs.openMsg('success', '添加成功！', function () {
                                table.reload('LAY-table'); //数据刷新
                                layer.close(index);        //关闭弹层
                            });
                        } else {
                            commonJs.openMsg('error', result.msg);
                        }
                    });
                });
                submit.trigger('click');
            });
        }
    };

    $('.layui-btn.layuiadmin-btn-operate').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    var cols = [{
        type: "checkbox",
        fixed: "left"
    }, {
        field: 'id',
        title: 'ID',
        width: 80
    }, {
        field: "subjectName",
        title: "科目",
        width: 200
    }, {
        field: 'name',
        title: '试卷名称'
    }, {
        field: 'questionType',
        title: '试题类型',
        templet: '#questionTypeTpl',
        width: 120
    }, {
        field: 'type',
        title: '试卷类型',
        templet: '#typeTpl',
        width: 120
    }, {
        field: 'paperDate',
        title: '试卷时间',
        width: 120,
        templet: function (d) {
            if (d.paperDate != null) {
                return util.toDateString(d.paperDate, "yyyy-MM-dd");
            } else {
                return ''
            }
        }
    }, {
        title: "操作",
        width: 350,
        align: "center",
        fixed: "right",
        toolbar: "#table-btn-operate"
    }];

    //初始化layui的table
    commonJs.tableRender(table, cols, prefix + "/list");

    //初始化layui的table的tool操作
    table.on("tool(LAY-table)", function (e) {
        if ("del" === e.event) {
            commonJs.openConfirm('确定删除吗？', function () {
                commonJs.openAjax(prefix + "/remove", {"id": e.data.id}, function (result) {
                    if (result.code == 0) {
                        commonJs.openMsg('success', '删除成功！', function () {
                            table.reload('LAY-table'); //数据刷新
                        });
                    } else {
                        commonJs.openMsg('error', result.msg);
                    }
                });
            });
        } else if ("edit" === e.event) {
            commonJs.openDialog('编辑', prefix + '/edit/' + e.data.id, {}, [], function (index, layero) {
                var iframeWindow = window['layui-layer-iframe' + index]
                    , submitID = 'LAY-operate-submit'
                    , submit = layero.find('iframe').contents().find('#' + submitID);

                //监听提交
                iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                    commonJs.openAjax(prefix + "/update", data, function (result) {
                        if (result.code == 0) {
                            commonJs.openMsg('success', '编辑成功！', function () {
                                table.reload('LAY-table'); //数据刷新
                                layer.close(index);        //关闭弹层
                            });
                        } else {
                            commonJs.openMsg('error', result.msg);
                        }
                    });
                });
                submit.trigger('click');
            });
        } else if ("manage" === e.event) {
            commonJs.openTabsPage(ctx + '/manage/paper/paperQuestion/' + e.data.id, 'ID:' + e.data.id + '-题目管理');
        } else if ("view" === e.event) {
            commonJs.openDialog('试卷查看', ctx + '/manage/paper/paper/view/' + e.data.id, {'btnHide': true, 'dialogArea': ['60%', '80%']}, []);
        }
    });
});